package com.yxym.homework;

public class homework06 {
    public static void main(String[] args) {
        Person tang = new Person("唐僧",new Horse());
        tang.common();
        tang.passRiver();
        tang.common();
        tang.common();
        tang.passRiver();
        tang.special();//遇到火焰山
    }
}

interface Vehicle{
    void work();
}

class Horse implements Vehicle{

    @Override
    public void work() {
        System.out.println("通常情况下用Horse作为交通工具...");
    }
}

class Boat implements Vehicle{

    @Override
    public void work() {
        System.out.println("遇到河流就用船Boat作为交通工具...");
    }
}
class Aircraft implements Vehicle{

    @Override
    public void work() {
        System.out.println("遇到火焰山用Aircraft作为交通工具...");
    }
}

class VehiclesFactory{
    //马儿始终只有一匹（java设计模式中的单例模式）
    private static Horse horse = new Horse();//饿汉式
    public static Horse getHorse(){
        return horse;
        //return new Horse();
    }

    public static Boat getBoat(){
        return new Boat();
    }
    public static Aircraft getAircraft(){
        return new Aircraft();
    }
}

class Person{
    private String name ;
    private Vehicle vehicle;
    public Person(String name, Vehicle vehicle) {
        this.name = name;
        this.vehicle = vehicle;
    }
    public void passRiver(){
        //判断当前的vehicle诺为Boat，就返回船的对象
        if (!(vehicle instanceof Boat)){
            vehicle = VehiclesFactory.getBoat();
        }
        vehicle.work();//接口调用
    }
    public void common(){
        //判断当前的工具是否为空
        if (!(vehicle instanceof Horse)){
            //判断当前的vehicle诺为Horse，就返回马的对象
            vehicle = VehiclesFactory.getHorse();
        }
        vehicle.work();//接口调用
//        Horse horse = VehiclesFactory.getHorse();
//        horse.work();
    }
    public void special(){
        //判断当前的vehicle诺为Aircraft，就返回飞机的对象
        if (!(vehicle instanceof Aircraft)){
            vehicle = VehiclesFactory.getAircraft();
        }
        vehicle.work();//接口调用
    }
}
//实例化Person对象“唐僧”，要求一般情况下用Horse作为交通工具，
// 遇到大河时用船当做工具
